package com.shi.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;

@Component
public class RedisUtil {

    private static RedisTemplate redisTemplate;

    @Resource
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

    public static void set(String key, Object val,Duration duration){
        redisTemplate.opsForValue().set(key,val,duration);
    }

    public static void set(String key, Object val){
        redisTemplate.opsForValue().set(key,val);
    }
    public static <T> T get(String key){
        Object o = redisTemplate.opsForValue().get(key);
        if (o == null) {
            return null;
        }
        return (T) o;
    }

    public static void delete(String key){
        redisTemplate.delete(key);
    }

    public static void expire(String key,Duration duration){
        redisTemplate.expire(key,duration);
    }

}
